﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;

namespace WPF_DbCenter.SqlServer.DataExchange
{
    /// <summary>
    /// Interaction logic for TargetServer.xaml
    /// </summary>
    public partial class TargetServer : Page
    {
        CoreEA.ICoreEAHander srcCore;
        List<string> tableList=null;
        public TargetServer(CoreEA.ICoreEAHander inSrcCore,List<string> inTableList)
        {
            srcCore = inSrcCore;
            tableList = inTableList;
            InitializeComponent();
        }

        private void butSync_Click(object sender, RoutedEventArgs e)
        {
            string server = txtServername.Text;
            string targetDb = this.txtDbName.Text;
            string username = this.txtUsername.Text;
            string pwd = passwordBox1.Password;
            bool isTrustedConn = (bool)chkIsTrustedConn.IsChecked;
            bool isSql05Express = (bool)chkIsSql05Expresss.IsChecked;

            if (string.IsNullOrEmpty(server) || string.IsNullOrEmpty(targetDb)
                || string.IsNullOrEmpty(username))
            {
                MessageBox.Show("Please input each element");
                return;
            }


            CoreEA.ICoreEAHander targetCore = new CoreEA.CoreE(CoreEA.CoreE.UsedDatabaseType.SqlServer).X_Handler;
            CoreEA.LoginDbInfo info = new CoreEA.LoginDbInfo(server, targetDb, "", username, pwd);
            if (isSql05Express)
            {
                info.CurSqlServerVersion = CoreEA.LoginDbInfo.SqlServerVersionCollection.SqlServer2005Express;
            }
            info.IsTrustedConn = (bool)chkIsTrustedConn.IsChecked;
            try
            {
                targetCore.Open(info);
                SqlBulkCopy cp = new SqlBulkCopy((SqlConnection)targetCore.GetConnection());
                foreach (string item in tableList)
                {
                    if(targetCore.GetTableListInDatabase(targetDb).Contains(item))
                    {
                        cp.DestinationTableName = item;
                        DataRowCollection coll= srcCore.GetAllDataFromTable(item).Tables[0].Rows;
                        DataRow[] rows=new DataRow[coll.Count];
                        int i=0;
                        foreach (DataRow subRow in coll)
                        {
                            rows[i] = subRow;
                        }

                        cp.WriteToServer(rows);
                        MessageBox.Show("Complete");
                    }
                    else
                    {
                        //Create New Table and do .
                    }
                }

            }
            catch (Exception ee)
            {
                XLCS.Common.ProcessException.DisplayErrors(ee);
            }
        }

    }
}
